package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.client.CartInfoFeignClient;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 李旭
 * @date 2022/1/17 9:23
 * @Description:
 */
@Controller
public class CartInfoController {

    @Autowired
    private CartInfoFeignClient cartInfoFeignClient;
    //加入购物车   添加商品到购物车中
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
        //只负责添加
        cartInfoFeignClient.addCart(skuId,skuNum);//远程调用
        redirectAttributes.addAttribute("sId",skuId);
        redirectAttributes.addAttribute("sNum",skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";
    }
    //加入购物车  跳转页面
    @GetMapping("/toCart.html")
    public String toCart(HttpServletRequest request,Long sId,Integer sNum){
        //查询
        CartInfo cartInfo = cartInfoFeignClient.toCart(sId,sNum);//远程调用
        //跳转到加入成功页面
        request.setAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }

}
